let query = require("../../db/mysql.js");
const express = require('express');
const Router = express.Router();
const {create,verify} = require("../token");

//注册
Router.post('/register', async (req, res) => {
    let { username, userpass } = req.body;
    let data = {};
    try {
        let sql = `insert into user(username,userpass) value('${username}','${userpass}')`;
        let p = await query(sql);
        if (p.affectedRows) {
            data = {
                code: 2000,
                flag: true,
                message: '注册成功',
            }
        } else {
            data = {
                code: 3000,
                flag: false,
                message: '注册失败',
            }
        }
        res.send(data)
    } catch (err) {
        data = {
            code: 5000,
            flag: false,
            message: '服务器出错',
        }
    }
})
//添加用户
Router.post('/addUser', async (req, res) => {
    let { username, userpass,phonenum,address } = req.body;
    let data = {};
    try {
        let sql = `insert into user(username,userpass,phonenum,address) value('${username}','${userpass}','${phonenum}','${address}')`;
        let p = await query(sql);
        if (p.affectedRows) {
            data = {
                code: 2000,
                flag: true,
                message: '添加成功',
            }
        } else {
            data = {
                code: 3000,
                flag: false,
                message: '添加失败',
            }
        }
        res.send(data)
    } catch (err) {
        data = {
            code: 5000,
            flag: false,
            message: '服务器出错',
        }
    }
})
//登录
Router.post('/login', async (req, res) => {
    let { username, userpass } = req.body;
    let data = {};
    try {
        let sql = `select * from user where username='${username}' and userpass='${userpass}'`;
        let p = await query(sql);
        if (p.length) {
            let token = create({ username: username, userid: p[0].userid });
            data = {
                code: 2000,
                flag: true,
                message: '登录成功',
                token:token,
                data: p[0]
            }
        } else {
            data = {
                code: 3000,
                flag: false,
                message: '登录失败',
            }
        }
        res.send(data)
    } catch (err) {
        data = {
            code: 5000,
            flag: false,
            message: '服务器出错',
        }
    }
})

//验证token
Router.get("/verify",(req,res)=>{
    let {token} = req.query;
    let result = verify(token);
    let data ={};
    if(result){
        data = {
            code:2000,
            flag:true,
            message:'验证成功'
        }
    }else{
        data = {
            code:3000,
            flag:false,
            message:'验证失败'
        }
    }
    res.send(data);
})
//检测用户名是否存在
Router.get('/checkUser', async (req, res) => {
    let { username } = req.query;
    let data = {};
    try {
        let sql = `select * from user where username='${username}'`;
        let p = await query(sql);
        if (p.length) {
            data = {
                code: 2000,
                flag: true,
                message: '用户存在',
            }
        } else {
            data = {
                code: 3000,
                flag: false,
                message: '用户不存在',
            }
        }
        res.send(data)
    } catch (err) {
        data = {
            code: 5000,
            flag: false,
            message: '服务器出错',
        }
    }
})

//退出登录
Router.get('/logout', async (req, res) => {
    let { username } = req.query;
    let data = {};
    try {
        let sql = `select * from user where username='${username}'`;
        let p = await query(sql);
        if (p.length) {
            data = {
                code: 2000,
                flag: true,
                message: '用户存在',
            }
        } else {
            data = {
                code: 3000,
                flag: false,
                message: '用户不存在',
            }
        }
        res.send(data)
    } catch (err) {
        data = {
            code: 5000,
            flag: false,
            message: '服务器出错',
        }
    }
})

//无传conditions 获取所有用户  有传
Router.get('/getUser', async (req, res) => {
    let { conditions } = req.query;
    conditions = conditions || '' ;
    let data = {};
    try {
        let sql = `select * from user where username like '%${conditions}%'`;
        let p = await query(sql);
        if (p.length) {
            data = {
                code: 2000,
                flag: true,
                message: '获取成功',
                data: p
            }
        } else {
            data = {
                code: 3000,
                flag: false,
                message: '获取失败',
            }
        }
        res.send(data)
    } catch (err) {
        data = {
            code: 5000,
            flag: false,
            message: '服务器出错',
        }
    }
})

//分页获取 用户
Router.get('/padinguser',async (req, res) => {
    let { page,size } = req.query;
    page = page || 1 ;
    size = size || 5 ;
    let data = {};
    let index = (page-1)*size;
    try {
        let sql = `select * from user where userid limit ${index},${size}`;
        let sqltotal = `select * from user`;
        let p = await query(sql);
        let arr = await query(sqltotal);
        if (p.length) {
            data = {
                code: 2000,
                flag: true,
                message: '获取成功',
                total:arr.length,
                page,//当前页
                size,//一页多少条
                data: p
            }
        } else {
            data = {
                code: 3000,
                flag: false,
                message: '获取失败',
            }
        }
        res.send(data)
    } catch (err) {
        data = {
            code: 5000,
            flag: false,
            message: '服务器出错',
        }
    }
})

//删除用户
Router.delete('/delUser', async (req, res) => {
    let { username } = req.body;
    let data = {};
    try {
        let sql = `delete from user where username='${username}'`;
        let p = await query(sql);
        if (p.affectedRows) {
            data = {
                code: 2000,
                flag: true,
                message: '删除成功',
            }
        } else {
            data = {
                code: 3000,
                flag: false,
                message: '删除失败',
            }
        }
        res.send(data)
    } catch (err) {
        data = {
            code: 5000,
            flag: false,
            message: '服务器出错',
        }
    }
})

//修改密码
Router.put('/updataPsw', async (req, res) => {
    let { username, newPsw } = req.body;
    let data = {};
    try {
        let sql = `update user set userpass='${newPsw}' where username='${username}'`;
        let p = await query(sql);
        if (p.affectedRows) {
            data = {
                code: 2000,
                flag: true,
                message: '修改成功',
            }
        } else {
            data = {
                code: 3000,
                flag: false,
                message: '修改失败',
            }
        }
        res.send(data)
    } catch (err) {
        data = {
            code: 5000,
            flag: false,
            message: '服务器出错',
        }
    }
})

//检测密码是否正确
Router.post('/checkPsw', async (req, res) => {
    let { username, userpass } = req.body;
    let data = {};
    try {
        let sql = `select * from user where username='${username}' and userpass='${userpass}'`;
        let p = await query(sql);
        if (p.length) {
            data = {
                code: 2000,
                flag: true,
                message: '密码正确',
            }
        } else {
            data = {
                code: 3000,
                flag: false,
                message: '密码错误',
            }
        }
        res.send(data)
    } catch (err) {
        data = {
            code: 5000,
            flag: false,
            message: '服务器出错',
        }
    }
})

//修改用户信息
Router.put('/editUser/:userid', async (req, res) => {
    let str = ''
    let {userid} = req.params;
    for(let key in req.body){
        str += key + "=" + `'${req.body[key]}'` + ","
    }
    str = str.slice(0,-1);
    let data = {};
    try {
        let sql = `update user set ${str} where userid=${userid}`;
        let p = await query(sql);
        if (p.affectedRows) {
            data = {
                code: 2000,
                flag: true,
                message: '修改成功',
            }
        } else {
            data = {
                code: 3000,
                flag: false,
                message: '修改错误',
            }
        }
        res.send(data)
    } catch (err) {
        data = {
            code: 5000,
            flag: false,
            message: '服务器出错',
        }
    }
})

//修改手机、地址
Router.post('/addPhoneAndAddress', async (req, res) => {
    let { username, phonenum,address } = req.body;
    // console.log(username, phonenum,address);
    let data = {};
    try {
        let sql = `update user set phonenum='${phonenum}',address='${address}' where username='${username}'`;
        // console.log(sql);
        let p = await query(sql);
        // console.log(p);
        if (p.affectedRows) {
            data = {
                code: 2000,
                flag: true,
                message: '修改成功',
            }
        } else {
            data = {
                code: 3000,
                flag: false,
                message: '修改失败',
            }
        }
        res.send(data)
    } catch (err) {
        data = {
            code: 5000,
            flag: false,
            message: '服务器出错',
        }
    }
})
module.exports = Router;